package com.css.fxfzxczh.modules.fieldWorkManage.noticeManage.service.Impl;

import com.css.fxfzxczh.constants.YNEnum;
import com.css.fxfzxczh.modules.fieldWorkManage.InvestigationGroupManage.entity.InvestigationTeam;
import com.css.fxfzxczh.modules.fieldWorkManage.noticeManage.entity.Notice;
import com.css.fxfzxczh.modules.fieldWorkManage.noticeManage.entity.NoticeRange;
import com.css.fxfzxczh.modules.fieldWorkManage.noticeManage.entity.Team;
import com.css.fxfzxczh.modules.fieldWorkManage.noticeManage.param.AuditNotice;
import com.css.fxfzxczh.modules.fieldWorkManage.noticeManage.param.NoticePageParam;
import com.css.fxfzxczh.modules.fieldWorkManage.noticeManage.param.NoticeSaveParam;
import com.css.fxfzxczh.modules.fieldWorkManage.noticeManage.repository.NoticeRepository;
import com.css.fxfzxczh.modules.fieldWorkManage.noticeManage.service.NoticeService;
import com.css.fxfzxczh.modules.fieldWorkManage.noticeManage.vo.NoticeSaveParamVO;
import com.css.fxfzxczh.modules.fieldWorkManage.noticeManage.vo.TeamVO;
import com.css.fxfzxczh.modules.synthesizeInfoManage.knowledgeManagement.entity.KnowledgeManagement;
import com.css.fxfzxczh.util.CreateGson;
import com.css.fxfzxczh.util.PlatformDateUtils;
import com.css.fxfzxczh.web.PlatformSessionContext;
import com.css.fxfzxczh.zcpt.sys.entity.SUser;
import com.css.fxfzxczh.zcpt.sys.service.SUserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.stream.Collectors;

/**
 * @Author: zhangSongRui
 * @CreateTime: 2023/02/02
 * @Description: 通知管理
 */
@Service
public class NoticeServiceImpl implements NoticeService {

    @Resource
    private NoticeRepository noticeRepository;
    @Resource
    private SUserService sUserService;

    @Override
    public Map<String, Object> getPage(NoticePageParam queParams, int curPage, int pageSize) {
        return noticeRepository.getPage(queParams, curPage, pageSize);
    }

    @Override
    public void save(NoticeSaveParam param) {
        param.setXnmId(UUID.randomUUID().toString().replaceAll("-", ""));
        param.setDelFlag(YNEnum.N.toString());
        String userID = PlatformSessionContext.getUserID(); // 获取当前登录用户
        param.setCreateUser(userID);
        param.setCreateTime(PlatformDateUtils.getCurrentTimestamp());
        ArrayList<NoticeRange> noticeRanges = new ArrayList<>();
        //如果范围为全体,则为所有现场人员
        if (param.getNoticeRange().equals("全部现场人员")) {
            //查询该地震事件下所有人员
            List<String> all = noticeRepository.findAllByXsemId(param.getXsemId());
            for (String s : all) {
                NoticeRange noticeRange = new NoticeRange();
                noticeRange.setTel(s);
                noticeRange.setXnmId(param.getXnmId());
                noticeRange.setId(UUID.randomUUID().toString().replaceAll("-", ""));
                noticeRange.setDelFlag(YNEnum.N.toString());
                noticeRanges.add(noticeRange);
            }
        }
        //如果范围为所有调查组人员,则为所有组长与组员
        if (param.getNoticeRange().equals("全部现场调查人员")) {
            //获取该地震事件下所有调查组组长电话号码
            List<String> leaderTel = noticeRepository.queryLeaderTelByXsemId(param.getXsemId());
            //获取该地震事件下所有调查组组员电话号码
            List<String> memberTel = noticeRepository.queryMemberTelByXsemId(param.getXsemId());
            leaderTel.addAll(memberTel);
            //去重
            List<String> myList = leaderTel.stream().distinct().collect(Collectors.toList());
            for (String s : myList) {
                NoticeRange noticeRange = new NoticeRange();
                noticeRange.setTel(s);
                noticeRange.setXnmId(param.getXnmId());
                noticeRange.setId(UUID.randomUUID().toString().replaceAll("-", ""));
                noticeRange.setDelFlag(YNEnum.N.toString());
                noticeRanges.add(noticeRange);
            }
        }
        //如果为按调查组勾选,则根据传入调查组id进行查询
        if (param.getNoticeRange().equals("按调查组勾选")) {
            //获取组长rel
            List<Team> teamList = param.getTeamList();
            ArrayList<String> list = new ArrayList<>();
            for (Team investigationTeam : teamList) {
                investigationTeam.setXnmId(param.getXnmId());
                //根据调查组id获取组员tel
                List<String> memberTel = noticeRepository.queryMemberTelById(investigationTeam.getXitId());
                list.add(investigationTeam.getLeaderPhone());
                list.addAll(memberTel);
            }
            //被选中调查组入库
            noticeRepository.saveTeam(teamList);
            //去重
            List<String> myList = list.stream().distinct().collect(Collectors.toList());
            for (String s : myList) {
                NoticeRange noticeRange = new NoticeRange();
                noticeRange.setTel(s);
                noticeRange.setXnmId(param.getXnmId());
                noticeRange.setId(UUID.randomUUID().toString().replaceAll("-", ""));
                noticeRange.setDelFlag(YNEnum.N.toString());
                noticeRanges.add(noticeRange);
            }
        }
        //保存tel到中间表
        noticeRepository.saveAll(noticeRanges);
        //保存通知
        noticeRepository.save(param);
    }

    @Override
    public void deleteById(String id) {
        noticeRepository.deleteById(id);
        //删除范围
        noticeRepository.deleteRange(id);
        //删除被选中的调查组
        noticeRepository.deleteTeam(id);
    }

    @Override
    public void updateById(NoticeSaveParam param) {
        String userID = PlatformSessionContext.getUserID(); // 获取当前登录用户
        param.setUpdateUser(userID);
        param.setUpdateTime(PlatformDateUtils.getCurrentTimestamp());
        //如果范围为全体,则为所有现场人员
        if (param.getNoticeRange()!=null){
            ArrayList<NoticeRange> noticeRanges = new ArrayList<>();
            noticeRepository.deleteRange(param.getXnmId());
            if (param.getNoticeRange().equals("全部现场人员")) {
                //查询该地震事件下所有人员
                List<String> all = noticeRepository.findAllByXsemId(param.getXsemId());
                for (String s : all) {
                    NoticeRange noticeRange = new NoticeRange();
                    noticeRange.setTel(s);
                    noticeRange.setXnmId(param.getXnmId());
                    noticeRange.setId(UUID.randomUUID().toString().replaceAll("-", ""));
                    noticeRanges.add(noticeRange);
                }
            }
            //如果范围为所有调查组人员,则为所有组长与组员
            if (param.getNoticeRange().equals("全部现场调查人员")) {
                //获取该地震事件下所有调查组组长电话号码
                List<String> leaderTel = noticeRepository.queryLeaderTelByXsemId(param.getXsemId());
                //获取该地震事件下所有调查组组员电话号码
                List<String> memberTel = noticeRepository.queryMemberTelByXsemId(param.getXsemId());
                leaderTel.addAll(memberTel);
                //去重
                List<String> myList = leaderTel.stream().distinct().collect(Collectors.toList());
                for (String s : myList) {
                    NoticeRange noticeRange = new NoticeRange();
                    noticeRange.setTel(s);
                    noticeRange.setXnmId(param.getXnmId());
                    noticeRange.setId(UUID.randomUUID().toString().replaceAll("-", ""));
                    noticeRanges.add(noticeRange);
                }
            }
            //如果为按调查组勾选,则根据传入调查组id进行查询
            if (param.getNoticeRange().equals("按调查组勾选")) {
                //获取组长rel
                List<Team> teamList = param.getTeamList();
                ArrayList<String> list = new ArrayList<>();
                for (Team investigationTeam : teamList) {
                    investigationTeam.setXnmId(param.getXnmId());
                    //根据调查组id获取组员tel
                    List<String> memberTel = noticeRepository.queryMemberTelById(investigationTeam.getXitId());
                    list.add(investigationTeam.getLeaderPhone());
                    list.addAll(memberTel);
                }
                //删除之前选中的调查组
                noticeRepository.deleteTeam(param.getXnmId());
                //保存现在的调查组
                noticeRepository.saveTeam(teamList);
                //去重
                List<String> myList = list.stream().distinct().collect(Collectors.toList());
                for (String s : myList) {
                    NoticeRange noticeRange = new NoticeRange();
                    noticeRange.setTel(s);
                    noticeRange.setXnmId(param.getXnmId());
                    noticeRange.setId(UUID.randomUUID().toString().replaceAll("-", ""));
                    noticeRanges.add(noticeRange);
                }
            }
            //保存tel到中间表
            noticeRepository.saveAll(noticeRanges);
        }
        noticeRepository.updateById(param);
    }

    @Override
    public NoticeSaveParamVO findById(String id) {
        NoticeSaveParamVO noticeSave = noticeRepository.findById(id);
        List<TeamVO> investigation = noticeRepository.queryTeam(id);
        noticeSave.setTeamList(investigation);
        return noticeSave;
    }

    //审核任务
    public String auditNotice(List<AuditNotice> list) {
        for (AuditNotice auditNotice : list) {
            auditNotice.setNoticeCode(getCode(auditNotice.getXsemId()).getNoticeCode());
            NoticeSaveParam noticeSaveParam = new NoticeSaveParam();
            BeanUtils.copyProperties(auditNotice,noticeSaveParam);
            noticeRepository.save(noticeSaveParam);
            //获取调查员tel
            String tel=noticeRepository.queryTelByName(auditNotice.getBeNotified(),auditNotice.getXsemId());
            NoticeRange noticeRange = new NoticeRange();
            noticeRange.setTel(tel);
            noticeRange.setXnmId(auditNotice.getXnmId());
            noticeRange.setDelFlag(YNEnum.N.toString());
            noticeRange.setId(UUID.randomUUID().toString().replaceAll("-", ""));
            ArrayList<NoticeRange> noticeRanges = new ArrayList<>();
            noticeRanges.add(noticeRange);
            noticeRepository.saveAll(noticeRanges);
        }
        return "ok";
    }

    @Override
    public Notice getCode(String xsemId) {
        Notice notice = new Notice();
        String user = sUserService.getSUser(PlatformSessionContext.getUserID());
        SUser sUser = CreateGson.createGson().fromJson(user, SUser.class);
        notice.setCreateUser(sUser.getUserName());
        notice.setCreateTime(PlatformDateUtils.getCurrentTimestamp());
        notice.setSponsor(sUser.getUserName());
        notice.setNoticeTime(PlatformDateUtils.getCurrentTimestamp());
        String code;
        String PersonCode;
        code = noticeRepository.getCode(xsemId);
        String XsemCode =noticeRepository.getXsemCode(xsemId);
        PersonCode = XsemCode+"TZ";
        if (StringUtils.isBlank(code)) {
            PersonCode = PersonCode + "001";
        } else {
            String[] ries = code.split("TZ");
            String next = getNext(ries[1]);
            PersonCode = PersonCode + next;
        }
        notice.setNoticeCode(PersonCode);
        return notice;
    }

    //获取所有的调查组根据地震事件id
    @Override
    public List<InvestigationTeam> getItemAll(String xsemId) {
        return noticeRepository.getItemAll(xsemId);
    }

    /****
     * 从哪里开始计数 比如startValue=2，则会从003,004,005等
     * @param startValue
     * @return
     */
    public String getNext(String startValue) {
        Integer i = Integer.valueOf(startValue);
        String format = String.format("%03d", i + 1);
        return format;
    }
}
